package com.archer.datasource;

import org.apache.commons.dbcp.BasicDataSource;

import javax.sql.DataSource;
import java.util.Properties;

public class DBCPDataSourceFactory {
    private BasicDataSource datasource = null;

    public DBCPDataSourceFactory() {
        this.datasource = new BasicDataSource();
    }

    public DataSource getDataSource() {
        return datasource;
    }

    public void setProperties(Properties ps) {
        datasource.setDriverClassName(ps.getProperty("driver"));
        datasource.setUrl(ps.getProperty("url"));
        datasource.setUsername(ps.getProperty("username"));
        datasource.setPassword(ps.getProperty("password"));
        datasource.setInitialSize(Integer.parseInt(ps.getProperty("initialSize", "0")));
        datasource.setMaxActive(Integer.parseInt(ps.getProperty("maxActive", "10")));
        datasource.setMaxIdle(Integer.parseInt(ps.getProperty("maxIdle", "5")));
        datasource.setMinIdle(Integer.parseInt(ps.getProperty("minIdle", "1")));
        datasource.setMaxWait(Long.parseLong(ps.getProperty("maxwait", "5000")));
        datasource.setMinEvictableIdleTimeMillis(Long.parseLong(ps.getProperty("minEvictableIdleTime", "30000")));
        datasource.setTimeBetweenEvictionRunsMillis(Long.parseLong(ps.getProperty("betweenEvictionRunsMillis", "30000")));
    }
}
